/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.CheckLocationNameRepeatException;
import com.huibur.devicecheck.model.param.CheckLocationParam;
import com.huibur.devicecheck.model.po.CheckLocationPO;
import com.huibur.devicecheck.model.query.CheckLocationQuery;
import com.huibur.devicecheck.model.vo.CheckLocationVO;
import com.huibur.devicecheck.model.vo.LocationStaVO;
import com.huibur.devicecheck.model.vo.LocationVO;
import com.huibur.devicecheck.request.ListRes;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.ICheckLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
@RequestMapping("/checkLocation")
@Api(value = "位置管理",tags = {"位置管理"})
@Slf4j
public class CheckLocationController {

    @Autowired
    ICheckLocationService iCheckLocationService;

    @PostMapping("")
    @ApiOperation(value = "新增位置",response = ObjRes.class)
    public ObjRes<Void> insert(@RequestBody CheckLocationParam checkLocationParam) {
        try {
            return iCheckLocationService.insert(checkLocationParam);
        } catch (CheckLocationNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.CHECK_LOCATION_NAME_REPEAT.getCode(), ResultCodeEnum.CHECK_LOCATION_NAME_REPEAT.getLabel());
        }
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除地址",response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id) {
        return new ObjRes<Boolean>().data(iCheckLocationService.deleteById(id));
    }

    /**
     * 编辑地址
     * @param id
     * @param checkLocationParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑地址",response = ObjRes.class)
    public ObjRes<Void> updateLocation(@PathVariable Long id,@RequestBody CheckLocationParam checkLocationParam) {
        try {
            return iCheckLocationService.updateLocation(id,checkLocationParam);
        } catch (CheckLocationNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.CHECK_LOCATION_NAME_REPEAT.getCode(), ResultCodeEnum.CHECK_LOCATION_NAME_REPEAT.getLabel());
        }
    }

    /**
     * 地址列表(查询并分页)
     * @param checkLocationQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "地址列表(查询并分页)",response = PageRes.class)
    public PageRes<CheckLocationVO> selectLocationList(CheckLocationQuery checkLocationQuery) {
        return iCheckLocationService.selectLocationList(checkLocationQuery);
    }

    /**
     * 根据ID来查询子级
     * @param id
     * @return
     */
    @GetMapping("/selectListById/{id}")
    @ApiOperation(value = "根据ID来查询子级")
    public ListRes<CheckLocationPO> selectById(@PathVariable Long id) {
        return new ListRes<CheckLocationPO>().data(iCheckLocationService.selectByParentId(id));
    }

    /**
     * 查询所有的节点
     * @param id
     * @return
     */
    @GetMapping("/selectAllLocation")
    @ApiOperation(value = "查询所有的节点",response = ListRes.class)
    public ListRes<LocationVO> selectAllLocation(@RequestParam(defaultValue = "0") Long id) {
        return new ListRes<LocationVO>().data(iCheckLocationService.selectAllLocation(id));
    }

    /**
     * 后台管理端首页统计图（地点统计）
     * @param locationId
     * @return
     */
    @GetMapping("/locationStatistics")
    @ApiOperation(value = "后台管理端首页统计图（地点统计）",response = ListRes.class)
    public ListRes<LocationStaVO> locationStatistics(@RequestParam(required = false) Long locationId) throws ParseException {
        return new ListRes<LocationStaVO>().data(iCheckLocationService.locationStatistics(locationId));
    }
}
